﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DiscoveryLogic.Common.Utilities;

namespace DiscoveryLogic.DataViz.NetworkDiagram.QueryControls
{
    /// <summary>
    /// 
    /// </summary>
    public partial class ObjectQueryContainer : UserControl
    {
        public event OnFieldFilterUpdated FieldFilterUpdated;

        private readonly Dictionary<string, FieldStat> _FieldStats;
        private readonly string _NetworkName;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="networkName"></param>
        /// <param name="fieldStats"></param>
        public ObjectQueryContainer(string networkName, Dictionary<string,FieldStat> fieldStats)
        {
            this._NetworkName = networkName;
            this._FieldStats = fieldStats;
            InitializeComponent();
        }

        private void ObjectQueryContainer_Load(object sender, EventArgs e)
        {
            this.p_QueryContainer.Controls.Clear();
            //this.p_QueryContainer.ColumnCount = 1;
            //this.p_QueryContainer.RowCount = this._FieldStats.Count;
            //this.p_QueryContainer.GrowStyle = TableLayoutPanelGrowStyle.AddRows;
            this.p_QueryContainer.Padding=new Padding(1,5,5,5);
            this.p_QueryContainer.VerticalScroll.Maximum = 0;
            this.p_QueryContainer.AutoScroll = true;

            foreach(string fieldName in this._FieldStats.Keys)
            {
                FieldStat stat = this._FieldStats[fieldName];
                IFieldFilter fieldFilterCtr = null;
                if (stat.DataType!=SimpleDataType.IntNumberType && 
                    stat.DistinctValueCount < 10 && 
                    stat.DistinctValueCount > 0)
                {
                    fieldFilterCtr = new OrdinalFilter();
                }
                else
                {
                    switch (stat.DataType)
                    {
                        case SimpleDataType.DateTimeType:
                            fieldFilterCtr = new DateBetweenFilter();
                            break;
                        case SimpleDataType.DecimalNumberType:
                        case SimpleDataType.DoubleNumberType:
                        case SimpleDataType.IntNumberType:
                            fieldFilterCtr = new NumberFieldFilter();
                            break;
                    }
                }
                if (fieldFilterCtr != null)
                {
                    fieldFilterCtr.Stat = stat;
                    fieldFilterCtr.NetworkName = this._NetworkName;
                    this.p_QueryContainer.Controls.Add((UserControl)fieldFilterCtr);
                    ((UserControl)fieldFilterCtr).Dock = DockStyle.Top;
                    fieldFilterCtr.FieldFilterUpdated +=new OnFieldFilterUpdated(HandleFieldFilterUpdatedEvent);
                }
            }
        }

        private void HandleFieldFilterUpdatedEvent(string networkName, string fieldName, object min, object max, List<string> allowedValues)
        {
            if (this.FieldFilterUpdated != null)
                this.FieldFilterUpdated(networkName, fieldName, min, max, allowedValues);
        }
    }
}
